(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:BalanceCtrl
     * @description
     * # BalanceCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('BalanceCtrl', BalanceCtrl);

    BalanceCtrl.$inject = ['$scope', 'dataService', 'helper', '$rootScope', '$location', 'sessionService'];

    function BalanceCtrl($scope, dataService, helper, $rootScope, $location, sessionService) {

        //打折活动列表
        $scope.listAction = function() {

            $scope.pageSize = 10;
            $scope.getList(1);
            $scope.$on('event:up-store-id', function() {
                $scope.getList(1);
            });

        }

        $scope.getList = function(pageNo) {
            var query = {
                order_by: "balance_activity_id:desc",
                page_no: pageNo,
                page_size: $scope.page_Size ? $scope.page_Size : 10,
            };

            query.query = { store_id: $rootScope.store_id };

            dataService.balanceGet(query).then(function(list) {
                if (list) {
                    for (var i in list.balance_activitys) {
                        list.balance_activitys[i].diff_time = $scope.diff_time(list.balance_activitys[i].start_time, list.balance_activitys[i].end_time)
                    }
                    $scope.balance_activitys = list.balance_activitys;
                    $scope.total_count = list.total_count;
                }
            });
        }

        //js计算两个时间的时间差
        $scope.diff_time = function(start_time, end_time) {
            var s_time = new Date().getTime();
            var start = new Date(start_time).getTime();
            var end = new Date(end_time).getTime();

            //当前时间是否大于活动结束时间
            if (s_time > end) {
                return 0;
            }

            //判断当前时间是否大于开始时间
            if (s_time > start && s_time < end) {
                var time = new Date().getTime();
            } else {
                var time = new Date(start_time).getTime();
            }

            //var diff = end_time * 1000 - start_time * 1000 //时间差的毫秒数  
            var diff = end - time;

            //计算出相差天数  
            var days = Math.floor(diff / (24 * 3600 * 1000));

            //计算出小时数  
            var leave1 = diff % (24 * 3600 * 1000); //计算天数后剩余的毫秒数  
            var hours = Math.floor(leave1 / (3600 * 1000));
            //计算相差分钟数  
            var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数  
            var minutes = Math.floor(leave2 / (60 * 1000));

            //计算相差秒数  
            var leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数  
            var seconds = Math.round(leave3 / 1000);

            var returnStr = seconds + "秒";
            if (minutes > 0) {
                returnStr = minutes + "分" + returnStr;
            }
            if (hours > 0) {
                returnStr = hours + "小时" + returnStr;
            }
            if (days > 0) {
                returnStr = days + "天" + returnStr;
            }
            return returnStr;
        }

        //充值活动添加页面
        $scope.addAction = function() {
            $scope.balance_activity_id = $location.search().balance_activity_id;

            if ($scope.balance_activity_id) {
                getOneData();
                $scope.$on('event:up-store-id', function() {
                    getOneData();
                });
            } else {
                $scope.balance_activity_data = {
                    "store_id": $rootScope.store_id,
                    "name": "充值活动",
                    "money": "100",
                    "money_gift": "100",
                    "start_time": $scope.getDateStr(0),
                    "end_time": $scope.getDateStr(7),
                };
            }
        }

        function getOneData() {
            var query = {
                query: {
                    balance_activity_id: $scope.balance_activity_id,
                    store_id: $rootScope.store_id,
                }
            };

            dataService.balanceGet(query).then(function(result) {
                if (result.balance_activitys) {
                    $scope.balance_activity_data = result.balance_activitys[0];
                }
            });
        }

        //活动添加
        $scope.balanceAdd = function() {
            if (!$scope.balance_activity_data.money) {
                helper.myAlert("需要设置充多少元");
                return false;
            }

            if (!$scope.balance_activity_data.money_gift) {
                helper.myAlert("需要设置送多少元");
                return false;
            }

            if ($scope.balance_activity_data.end_time < $scope.balance_activity_data.start_time) {
                helper.myAlert("活动结束时间不能小于活动开始时间");
                return false;
            }

            if ($scope.balance_activity_id) {
                dataService.balanceUpdate($scope.balance_activity_data).then(function(result) {
                    if (result.balance_activity_id) {
                        helper.myAlert("success", "#/balance/list");
                    } else {
                        helper.myAlert(result.errmsg);
                    }
                });
            } else {
                dataService.balanceAdd($scope.balance_activity_data).then(function(result) {
                    if (result.balance_activity_id) {
                        helper.myAlert("success", "#/balance/list");
                    } else {
                        helper.myAlert(result.errmsg);
                    }
                });
            }
        }

        //充值活动删除
        $scope.balanceDelete = function(balance_activity_id) {
            helper.myConfirm('确认删除吗？', function() {
                var data = { 'balance_activity_id': balance_activity_id };

                dataService.balanceDelete(data).then(function(result) {
                    if (result.balance_activity_id) {
                        $scope.getList(1);
                    } else {
                        helper.myAlert("error:" + result);
                    }
                });
            });
        }
        
        //活动时间计算
        $scope.getDateStr = function(addDayCount) {
            var dd = new Date();
            dd.setDate(dd.getDate() + addDayCount);
            var y = dd.getFullYear();
            var m = (dd.getMonth() + 1) < 10 ? "0" + (dd.getMonth() + 1) : (dd.getMonth() + 1);
            var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
            var h = dd.getHours();
            var i = dd.getMinutes();
            var s = dd.getSeconds();

            if (i < 10) {
                i = "0" + i;
            }
            var s = dd.getSeconds();
            if (s < 10) {
                s = "0" + s;
            }

            return y + "-" + m + "-" + d + " " + h + ":" + i + ":" + s;
        }
    }
})();